package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsCommend;
import cn.wolfcode.trip.base.query.NewsCommendQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsCommendService;
import cn.wolfcode.trip.base.utils.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("newsCommend")
public class NewsCommendController {
    @Autowired
    private INewsCommendService newsCommendService;
        //推荐资讯列表
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") NewsCommendQueryObject qo, Model model) {
        PageInfo<NewsCommend> query = newsCommendService.query(qo);
        model.addAttribute("pageInfo", query);

              return "/newsCommend/list";
    }


    //编辑或者更新资讯
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(NewsCommend newsCommend, MultipartFile multipartFile) {
        JSONResult result = new JSONResult();
        try{
//            //判断是否传入新的图片
//            if(multipartFile!=null&&multipartFile.getSize()>0){
//                String imagePath = UploadUtil.upload(multipartFile, UploadUtil.IMG_BASE_PATH + "/upload");
//                news.setCoverUrl(imagePath);
//            }
            newsCommendService.saveOrUpdate(newsCommend);

        }catch(Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }
}
